package com.deimos.sps;

import java.text.ParseException;
import java.util.Date;
import org.vast.ows.OWSException;
import org.vast.ows.sps.StatusReport;
import org.vast.ows.sps.SubmitRequest;
import org.vast.ows.sps.SubmitResponse;
import org.vast.ows.sps.StatusReport.RequestStatus;
import com.deimos.Notification;
import com.deimos.UtilsDeimos;
import com.deimos.dataBase.Event;
import com.deimos.dataBase.Sensor;
import com.deimos.dataBase.Task;

public class Submit extends GetFeasibility {
    // private SubmitResponse response;
    String task = null;

    public Submit(SubmitRequest request, Sensor sensor) throws ParseException, OWSException {
	super(request, null, Operation.SUBMITTED, sensor);
    }

    public SubmitResponse getResponse() throws OWSException {
	return (SubmitResponse) super.getResponse();
    }

    public void addTask(SubmitResponse response) {
	Task task = new Task();
	Event event = new Event();
	StatusReport report = response.getReport();
	task.setSensorId(request.getSensorID());
	task.setServiceType(response.getService());
	task.setTaskId(report.getTaskID());
	task.setTitle(report.getTitle());

	event.setRequestStatus(report.getRequestStatus());
	event.setTaskStatus(report.getTaskStatus());
	event.setDescription(report.getDescription());
	event.setUpdateDate(new Date());
	event.setEstimatedToC(report.getEstimatedToC());
	event.setResponseParameters(UtilsDeimos.getParameters(response));
	event.setRequestParameters(UtilsDeimos.getParameters(request));
	event.setFeasibilityStudy(report != null && report.getRequestStatus() == RequestStatus.Accepted);
	event.setBeginDate(begin);
	event.setEndDate(end);
	event.setMaxCloud(maxCloud);
	event.setOperation(Operation.SUBMITTED);

	task.addEvent(event);
	task.setSegments(segments);

	taskController.insertTask(task);
	event.setTask(task);
	Notification.notifyReport(event);
    }
}
